/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.stop.risk.base.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.stop.common.constant.enums.ParkStopModDictEnum;
import com.hopes.bizz.stop.risk.base.dto.LinePollMonitorFactorExcelDTO;
import com.hopes.bizz.stop.risk.base.entity.LinePollMonitorFactorEntity;
import com.hopes.bizz.stop.risk.base.entity.LinePollMonitorSiteEntity;
import com.hopes.bizz.stop.risk.base.mapper.LinePollMonitorFactorMapper;
import com.hopes.bizz.stop.risk.base.service.LinePollMonitorFactorService;
import com.hopes.bizz.stop.risk.base.service.LinePollMonitorSiteService;
import com.hopes.bizz.stop.risk.base.vo.LinePollMonitorFactorExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 监控点监控因子
 *
 * @author pig
 * @date 2024-02-21 11:08:27
 */
@Service
@RequiredArgsConstructor
public class LinePollMonitorFactorServiceImpl extends ServiceImpl<LinePollMonitorFactorMapper, LinePollMonitorFactorEntity> implements LinePollMonitorFactorService {

    private final LinePollMonitorSiteService linePollMonitorSiteService;

    @Override
    public ValidateImportBO<LinePollMonitorFactorExcelDTO> validateImport(List<LinePollMonitorFactorExcelDTO> excelDTOList) {
        ValidateImportBO<LinePollMonitorFactorExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<LinePollMonitorFactorExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public LinePollMonitorFactorEntity importDtoToEntity(LinePollMonitorFactorExcelDTO excelDto) {
        LinePollMonitorFactorEntity entity = new LinePollMonitorFactorEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public LinePollMonitorFactorExcelVO exportEntityToVo(LinePollMonitorFactorEntity entity) {
        LinePollMonitorFactorExcelVO excelVO = new LinePollMonitorFactorExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public void saveAndUpdateMonitor(LinePollMonitorFactorEntity linePollMonitorFactor) {
        save(linePollMonitorFactor);
        LinePollMonitorSiteEntity monitorSiteEntity = linePollMonitorSiteService.getById(linePollMonitorFactor.getMonitorSiteId());
        String monitorFactorNames = monitorSiteEntity.getMonitorFactorNames();
        if (StrUtil.isBlank(monitorFactorNames)) {
            monitorSiteEntity.setMonitorFactorNames(DictResolver.getItemLabelByValue(ParkStopModDictEnum.factor_name, linePollMonitorFactor.getFactorId()));
        } else {
            monitorSiteEntity.setMonitorFactorNames(String.join(",", monitorFactorNames, DictResolver.getItemLabelByValue(ParkStopModDictEnum.factor_name, linePollMonitorFactor.getFactorId())));
        }
        linePollMonitorSiteService.updateById(monitorSiteEntity);
    }

    @Override
    public void removeAndUpdateMonitor(Long id) {
        LinePollMonitorFactorEntity pollMonitorFactor = getById(id);
        Long monitorSiteId = pollMonitorFactor.getMonitorSiteId();
        LinePollMonitorSiteEntity linePollMonitorSite = linePollMonitorSiteService.getById(monitorSiteId);
        String monitorFactorNames = linePollMonitorSite.getMonitorFactorNames();
        String factorNames = monitorFactorNames.replace(DictResolver.getItemLabelByValue(ParkStopModDictEnum.factor_name, pollMonitorFactor.getFactorId()), "");
        if (factorNames.equals(",")) {
            factorNames = "";
        }
        linePollMonitorSite.setMonitorFactorNames(factorNames);
        linePollMonitorSiteService.updateById(linePollMonitorSite);
        removeById(id);
    }

    @Override
    public List<Tree<Long>> monitorFactorTree() {
        List<LinePollMonitorSiteEntity> siteEntityList = linePollMonitorSiteService.list();

        List<TreeNode<Long>> tree = siteEntityList.stream().map(item -> {
            TreeNode<Long> treeNode = new TreeNode<>();
            treeNode.setId(item.getId());
            treeNode.setParentId(0L);
            treeNode.setName(item.getName());
            return treeNode;
        }).collect(Collectors.toList());
        List<LinePollMonitorFactorEntity> monitorFactorEntityList = list();
        tree.addAll(
                monitorFactorEntityList.stream().map(item -> {
                    TreeNode<Long> treeNode = new TreeNode<>();
                    treeNode.setId(item.getId());
                    treeNode.setParentId(item.getMonitorSiteId());
                    treeNode.setName(item.getFactorIdStr());
                    return treeNode;
                }).collect(Collectors.toList())
        );

        return TreeUtil.build(tree, 0L);
    }
}
